#include "penquan.h"


#define FRAND   (((float)rand()-(float)rand())/RAND_MAX)


void penquan::InitializeParticle(int index)
{
    float r,a,b;
    r=FRAND*m_radius;
    a=FRAND*m_a_end;
    b=FRAND*m_b_end;

    m_particleList[index].m_size = PENQUAN_SIZE;
    m_particleList[index].m_weight =0.1;

    m_particleList[index].m_pos[0]=m_origin[0]+r*cos(a);
    m_particleList[index].m_pos[1]=m_origin[1]+r*abs(cos(b));
    m_particleList[index].m_pos[2]=m_origin[2]+r*sin(a);

    m_particleList[index].m_velocity = (m_particleList[index].m_pos-m_origin)/(m_particleList[index].m_pos-m_origin).norm();

    m_particleList[index].m_energy = PENQUAN_ENERGY + FRAND*PENQUAN_ENERGY_VAR;
}



void penquan::Update(float elapsedTime)
{
    elapsedTime*=10;
    for (int i = 0; i < m_numParticles; )
    {
        m_particleList[i].m_acceleration=m_force/m_particleList[i].m_weight;
        m_particleList[i].m_velocity += m_particleList[i].m_acceleration*elapsedTime;
        m_particleList[i].m_prevPos = m_particleList[i].m_pos;
        m_particleList[i].m_pos = m_particleList[i].m_pos + m_particleList[i].m_velocity * elapsedTime;

        if (m_particleList[i].m_energy<=0)
        {
            m_particleList[i] = m_particleList[--m_numParticles];
        }
        else
        {
            ++i;
        }
        m_particleList[i].m_energy--;

    }

    m_accumulatedTime += elapsedTime;

    int newParticles = PENQUAN_PER_SEC * m_accumulatedTime;

    m_accumulatedTime -= 1.0f/(float)PENQUAN_PER_SEC * newParticles;

    Emit(newParticles);
}

void penquan::Render()
{
    //GLUquadric* obj = gluNewQuadric();
    static float m[16]=
                        {
                            1,0,0,0,
                            0,1,0,0,
                            0,0,1,0,
                            0,0,0,1
                        };
    glEnable(GL_TEXTURE_2D);

    Vector3f partPos;
    float size;

    // draw the quads
    glColor3f(0,1,0);
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    //glBegin(GL_QUADS);
    for (int i = 0; i < m_numParticles; ++i)
    {
        glPushMatrix();
        partPos = m_particleList[i].m_pos;
        glTranslatef(partPos[0],partPos[1],partPos[2]);
        glGetFloatv(GL_MODELVIEW_MATRIX,m);
        _shap[i].draw(m);
        glPopMatrix();
        //gluSphere(obj,0.2,8,8);

        /**
        size = m_particleList[i].m_size;
        glTexCoord2f(0.0, 1.0);
        glVertex3f(partPos[0], partPos[1], partPos[2]);
        glTexCoord2f(1.0, 1.0);
        glVertex3f(partPos[0] + size, partPos[1], partPos[2]);
        glTexCoord2f(1.0, 0.0);
        glVertex3f(partPos[0] + size, partPos[1] - size, partPos[2]);
        glTexCoord2f(0.0, 0.0);
        glVertex3f(partPos[0], partPos[1] - size, partPos[2]);
        **/
    }
    //glEnd();

    glDisable(GL_TEXTURE_2D);
}

void penquan::InitializeSystem()
{
    int i;
    m_force<<0,-0.4,0;
    _shap = new glu_sphere[m_maxParticles];
    for(i=0;i<m_maxParticles;i++)
    {
        _shap[i].init_sphere(0.1,8,8);
    }
    CParticleSystem::InitializeSystem();
}

void penquan::KillSystem()
{
    CParticleSystem::KillSystem();
}

